<?php

use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
use Illuminate\Support\Facades\DB;

class CreateBlogsTable extends Migration
{
    public function up()
    {   
        $this->create('feedbacks', '反馈表', function(Blueprint $table){
            $table->increments('id'); 

            ################################################
            $table->smallInteger('type')->default(1)->comment('反馈类型: 1-系统bug 2-翻译问题 3-其它问题');
            $table->text('content')->nullable(true)->comment('反馈内容');
            $table->string('photos')->comment('反馈图片');
            ################################################
            $table->timestamps();
            $table->engine = 'InnoDB';
        });
    }

    public function down()
    {
        // 2.添加表
        $this->drop([
            'feedbacks',
        ]);
        // 3.执行
        // php artisan migrate
    }

    /**
     * 表创建
     */
    private function create(string $tableName, string $tableComment, Closure $callback)
    {
        Schema::dropIfExists($tableName);
        Schema::create($tableName, $callback);
        DB::statement("ALTER TABLE `{$tableName}` comment '{$tableComment}'");
    }

    /**
     * 表删除
     */
    private function drop(array $tables)
    {
        foreach ($tables as $table) {
            Schema::dropIfExists($table);
        }
    }

    public function sqqq()
    {
        // 1.设计表
        $this->create('cates', '博客分类表', function(Blueprint $table){
            $table->increments('id'); 
            ################################################

            $table->string('cate_name')->nullable(false)->comment('分类名称');
            $table->smallInteger('sort')->default(0)->comment('排序');

            ################################################
            $table->timestamps();
            $table->engine = 'InnoDB';
        });

        $this->create('tags', '博客标签表', function(Blueprint $table){
            $table->increments('id'); 
            ################################################

            $table->string('tag_name')->nullable(false)->comment('标签名称');
            $table->smallInteger('sort')->default(0)->comment('排序');

            ################################################
            $table->timestamps();
            $table->engine = 'InnoDB';
        });

        // users table
        $this->create('blogs', '博客表', function(Blueprint $table){
            $table->increments('id'); 
            ################################################

            $table->integer('user_id')->comment('会员ID');
            $table->smallInteger('cate_id')->comment('分类ID');
            $table->string('title')->comment('标题');
            $table->string('headimg')->comment('头图片');
            $table->longText('content')->comment('内容');
            $table->timestamp('publish_at')->nullable(true)->comment('发布日期');
            $table->morphs('audit');

            ################################################
            $table->timestamps();
            $table->engine = 'InnoDB';
        });

        $this->create('blog_tag', '博客表', function(Blueprint $table){
            ################################################
            $table->integer('blog_id')->comment('会员ID');
            $table->integer('tag_id')->comment('会员ID');
            $table->primary(['blog_id', 'tag_id']);
            ################################################
            $table->engine = 'InnoDB';
        });

        $this->create('admins', '后台用户表', function(Blueprint $table){
            $table->increments('id'); 
            ################################################
            $table->string('nickname')->nullable(false)->default()->comment('昵称'); 
            $table->string('mobile', 20)->nullable(false)->default()->comment('手机号'); 
            $table->string('email')->nullable(false)->default()->comment('email'); 
            $table->string('password')->nullable(false)->default()->comment('密码'); 
            $table->string('avatar')->nullable(true)->default('http://iph.href.lu/30x30')->comment('头像'); 
            ################################################
            $table->timestamps();
            $table->engine = 'InnoDB';
        });

        $this->create('roles', '角色表', function(Blueprint $table){
            $table->increments('id'); 

            ################################################
            $table->string('role_name')->comment('角色名称');
            $table->longText('permissions')->comment('权限');
            $table->string('remark')->comment('备注');

            ################################################
            $table->timestamps();
            $table->engine = 'InnoDB';
        });

        $this->create('menus', '系统菜单表', function(Blueprint $table){
            $table->increments('id'); 

            ################################################
            $table->integer('pid')->default(-1)->comment('pid');
            $table->string('menu_title')->comment('菜单/权限名称');
            $table->string('menu_slug')->comment('菜单/权限标识');
            $table->string('menu_icon')->comment('菜单/权限图标');
            $table->string('menu_href')->comment('链接');
            $table->string('target')->default('_self')->comment('链接打开方式: _seft, ');
            $table->string('remark')->comment('备注');
            $table->integer('status')->comment('状态: 0-禁用 1-启用');
            $table->integer('sort')->comment('排序');
            $table->smallInteger('menu_type')->comment('菜单类型: -1-目录 0-菜单 1-按钮');

            ################################################
            $table->timestamps();
            $table->engine = 'InnoDB';
        });


        // member
        $this->create('members', '会员表', function(Blueprint $table){
            $table->increments('id'); 
            ################################################

            $table->string('nickname')->nullable(false)->default()->comment('昵称'); 
            $table->string('mobile', 20)->nullable(false)->default()->comment('手机号'); 
            $table->string('email')->nullable(false)->default()->comment('email'); 
            $table->string('password')->nullable(false)->default()->comment('密码'); 
            $table->tinyInteger('gender')->unsigned()->default(0)->comment('性别：0-女 1-男 2-保密');
            $table->smallInteger('age')->unsigned()->default()->comment('年龄');
            $table->string('avatar')->nullable(true)->default('http://iph.href.lu/30x30')->comment('头像'); 
            $table->text('phrase')->nullable(true)->comment('个性签名');

            ################################################
            $table->timestamps();
            $table->engine = 'InnoDB';
        });
    }
}
